

<?php 
	class User extends Admin_Controller{
		public function __construct(){ 
			parent::__construct(); 	
			$this->load->helper("url");
				
		}
		public function index(){ 								// action: index
			$this->_data['titlePage']= "list all User";
			$this->_data['loadPage']="user/index_view";			//link to index_view.php
			
			
							// PHAN TRANG 
			$this->load->library('pagination');
			$this->load->model("Muser");
											
			$config['base_url'] = base_url().'admin/user/index';
			$config['total_rows'] = $this->Muser->countALL();			// the number of rows
			$config['per_page'] = 3; 									// 3 record/page
			$start= $this->uri->segment(4);									
			$this->pagination->initialize($config);
			$this->_data['pagelink']= $this->pagination->create_links();
			$this->_data['info']=$this->Muser->listAllUser($config['per_page'],$start);				// xu ly trong Model va luu trong trong $info
			
			$this->_data['mess']= $this->session->flashdata('flash_mess');
			$this->load->view($this->_data['path'],$this->_data);			
					
			}
		public function add(){
			$this->load->helper("form");
			$this->load->library('form_validation');
			$this->form_validation->CI =& $this;				// using for "call_back"
			$this->_data['titlePage']= "ADD USERS";
			$this->_data['loadPage']="user/add_view";			//link to add_view.php
			

						// form_validation->set_rules
						
		
			$this->form_validation->set_rules('username','Username','required|min_length[4]|callback_check_user');
			$this->form_validation->set_rules('password','Password','required|matches[password]');
			$this->form_validation->set_rules('email','Email','required|valid_email|callback_check_email');
				
				if($this->form_validation->run() == TRUE){
							$data_insert = array (
								'username' => $this->input->post("username"),
								'password' => $this->input->post("password"),
								'email'=> $this->input->post("email"),
								'level'=> $this->input->post("level"),
								);
						$this->load->model("Muser");
						$this->Muser->insertdata($data_insert);																	// insert vao csdl  
						$this->session->set_flashdata('flash_mess','success');
						redirect(base_url().'admin/user/index');
				}
						$this->load->view($this->_data['path'],$this->_data);	
		} // end function add  
				
				
						// KIEM TRA su trung lap(user,email) trong DATABASE 
					public function check_user($user,$id){
							$this->load->model("Muser");
							if  ($this->Muser->checkUsername($user,$id) == FALSE) 	{					// FALSE 
								//$this->form_validation->set_message('check_user','USERNAME HAS BEEN registered');				// thong bao loi
								$this->form_validation->set_message('check_user','USERNAME HAS BEEN REGISTERED');
							
								return False;			// need to return false to alarm that"validation->run= true"
							}else {
								return TRUE;
								
							}
								
					}
					
					public function check_email($email,$id){
							$this->load->model("Muser");
							if  ($this->Muser->checkEmail($email,$id) == FALSE) 	{					// FALSE 
								$this->form_validation->set_message('check_email','EMAIL HAS BEEN REGISTERED');
								return False;	
							}else {
								return true;
								
							}
								
					}
					
					public function del(){
						$id= $this->uri->segment(4);
						$this->load->model("Muser");
						$this->Muser->deleteUser($id);
						$this->session->set_flashdata('flash_delete','DA DELETE THANH CONG ');
						redirect(base_url().'admin/user/index');
					
					}
					
					public function edit(){											// edit thanh vien
					
							$id= $this->uri->segment(4);      						// lay ID
							$this->load->model("Muser"); 
							$this->load->helper("form");
							$this->load->library('form_validation');
							$this->form_validation->CI =& $this;					// using for "call_back"
							$this->_data['titlePage']= "EDIT USERS";
							$this->_data['loadPage']="user/edit_view";				//link to edit_view.php
									
							$this->_data["info"] = $this->Muser->getUserById($id); 
							
							// form_validation->set_rules
						
							$this->form_validation->set_rules('username','Username','required|min_length[4]|callback_check_user');
							$this->form_validation->set_rules('password','Password','required|matches[password]');
							$this->form_validation->set_rules('email','Email','required|valid_email|callback_check_email');
								
								if($this->form_validation->run() == TRUE){
											$data_update = array (
												'id'=> $this->input->post("id"),
												'username' => $this->input->post("username"),
												'password' => $this->input->post("password"),
												'email'=> $this->input->post("email"),
												'level'=> $this->input->post("level")
												);
										$this->load->model("Muser");
										$this->Muser->updatedata($data_update);													// insert vao csdl  
										$this->session->set_flashdata('flash_mess','EDIT THANH CONG');
										redirect(base_url().'admin/user/index');
								}
										$this->load->view($this->_data['path'],$this->_data);	
			
									
					}// end EDIT USERS
	
	} // end class 

?> 